//提供动态配置文件服务
;
(function() {
	var role = require("./role");
	_str_to_js = function(str) {
		var wrap_str = JSON.stringify(str);
		return 'window.appConfig={error:' + wrap_str + '};alert(' + wrap_str + ')'
	}
	module.exports = {
		prefix: "/config",
		get: {
			//返回JS，注意，不能使用this.body= ，避免跨域封装解析
			"/config.json": function(req) {
				console.log("get config")
				var host = req.header("host") || "";
				var bus_id = req.header("bus_id");
				var res = this.response;

				if (!bus_id) {
					res.send(_str_to_js("所用域名无匹配商家，请联系官方人员进行确认"));
					return;
				}

				// try {
				// 	var bus = role.getBus(this);
				// } catch (e) {}
				if (!bus) {
					var bus = BusCon.get(bus_id);
				}
				if (!bus) {
					res.send(_str_to_js("找不到指定商家"));
					return;
				}
				if (bus.status !== BusCon.STATE.审核通过) {
					res.send(_str_to_js("商家未通过审核"));
					return;
				}
				if (bus.expiration_time < +new Date) {
					try {
						var loginer_bus = role.getBus(this)
					} catch (e) {}
					if (loginer_bus && loginer_bus._id === bus._id) { //如果是商家自己浏览这个过期的网站，还是可以浏览，但会被标注已经过期
						var is_bus_late = true;
					} else {
						res.send(_str_to_js("商家使用权已经到期"));
						return;
					}
				}
				var security_bus = $Object.clone(bus);

				var config = config_template(host);
				config.is_bus_late = is_bus_late; //是否过期
				config.bus_id = security_bus._id;
				config.site_info = {
					bus_id: security_bus._id,
					info: security_bus.info,
					config: security_bus.config,
					permission: security_bus.permission
				};
				res.send("window.appConfig=(function(){return " + JSON.stringifySecurity(config) + "}())");
			},
			"/config.bus.json": function(req) {
				var host = req.header("host") || "";
				console.log(host);
				var config = config_template(host);
				var res = this.response;
				try {
					var loginer_bus = role.getBus(this);
					config.bus_id = loginer_bus._id;
					config.site_info = {
						bus_id: loginer_bus._id,
						info: loginer_bus.info,
						config: loginer_bus.config,
						permission: loginer_bus.permission
					};
				} catch (e) {}
				res.send("window.appConfigBus=(function(){return " + JSON.stringifySecurity(config) + "}())");
			},
			"/config.public.json": function(req) {
				var host = req.header("host") || "";
				var config = config_public_template(host);
				this.body = "window.appConfigPub=(function(){return " + JSON.stringifySecurity(config) + "}())";
			},
			"/config.base.json": function(req) {
				var host = req.header("host") || "";
				var config = config_base_template(host);
				this.body = "window.appConfigBase=(function(){return " + JSON.stringifySecurity(config) + "}())";
			},
			"/config.admin.json": function(req) {
				var host = req.header("host") || "";
				// try {
				// var admin = role.getAdmin(this);
				// } catch (e) {
				// 	var res = this.response;
				// 	res.send(_str_to_js(e));
				// }
				var config = config_admin_template(host);
				this.body = "window.appConfigAdmin=(function(){return " + JSON.stringifySecurity(config) + "}())";
			},
			"/force/hook": function() {
				var random_version = Math.random().toString(36).substr(2) + (+new Date).toString(36)
				config_base_template.updateVersion(random_version);
				this.body = "强制更新版本号成功：" + random_version;
			}
		},
		post: {
			"/git/hook": function() {
				var body = this.request.body;
				var hook_info_str = body.hook;
				if (!hook_info_str) {
					this.body = "arguments error";
					return
				}
				try {
					var hook_info = JSON.parse(hook_info_str);
					console.log(hook_info);
					var rep_name = hook_info.push_data.user_name + "_" + hook_info.push_data.repository.name;
				} catch (e) {
					this.body = "parse error";
					return
				}
				//保存Git@OSC的版本更新信息
				fs.writeFileSync(__dirname + "/config.template/." + rep_name + ".hook.json", hook_info_str)
				this.body = hook_info.push_data.after;

				//更新配置文件中的版本信息
				console.log(rep_name);
				config_base_template.updateVersion(rep_name, hook_info.push_data.after);
			},
			"/git/hook/other": function() {

				var body = this.request.body;
				var hook_info_str = body.hook;
				if (!hook_info_str) {
					this.body = "arguments error";
					return
				}
				try {
					var hook_info = JSON.parse(hook_info_str);
					console.log(hook_info);
					var rep_name = hook_info.push_data.user_name + "_" + hook_info.push_data.repository.name;
				} catch (e) {
					this.body = "parse error";
					return
				}

				if (rep_name === "xuezi_dotnar_mall") {
					//更新代码
					child_process.exec("~/update_shangcheng.sh", function(error, stdout, stderr) {
						if (error !== null) {
							console.log('stderr: ' + stderr);
							console.log('获取商城前端代码出错: ' + error);
						}
						console.log('stdout: ' + stdout);
						console.log("获取商城前端代码完成");
					});
				}
			}
		}
	}
	var child_process = require("child_process");
	var role = require("./role");
	var config_template = require("./config.template/config.template");
	var config_base_template = require("./config.template/config.base.template");
	var config_public_template = require("./config.template/config.public.template");
	var config_admin_template = require("./config.template/config.admin.template");
	var BusCon = require("../../core/bus/_con");
	var helper = require("../../core/_include/helper");
	var fs = require("fs");
	var path = require("path");
	var tld = require('tldjs');
	var $Object = require("../../core/_include/$.Object");
}());